package com.zoranjoka.chordis.services;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.hibernate.HibernateSymbols;
import org.apache.tapestry5.hibernate.HibernateTransactionAdvisor;
import org.apache.tapestry5.hibernate.HibernateTransactionDecorator;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.MethodAdviceReceiver;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Match;

import com.zoranjoka.chordis.hibernate.DataAlbumHibernate;
import com.zoranjoka.chordis.hibernate.DataArtistHibernate;
import com.zoranjoka.chordis.hibernate.IDataAlbumService;
import com.zoranjoka.chordis.hibernate.IDataArtistService;

public class AppModule {
	public static void bind(ServiceBinder binder){
		binder.bind(IDataArtistService.class, DataArtistHibernate.class);
        binder.bind(IDataAlbumService.class,DataAlbumHibernate.class);
    }
    
    public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration){        
        configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
        configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
        configuration.add(SymbolConstants.APPLICATION_VERSION, "1.0-SNAPSHOT");
        configuration.add(HibernateSymbols.EARLY_START_UP, "true");
    }

    public static void contributeHibernateEntityPackageManager(Configuration<String> configuration){
        configuration.add("com.zoranjoka.chordis");
    }
    
    @Match({"*IDataArtist","*IDataAlbum"})
    public static <T> T decorateTransactionally(HibernateTransactionDecorator decorator, Class<T> serviceInterface,T delegate, String serviceId) {
        return decorator.build(serviceInterface, delegate, serviceId);
    }
    @Match({"*Service"})
    public static void adviseTransactions(HibernateTransactionAdvisor advisor, MethodAdviceReceiver receiver){
        advisor.addTransactionCommitAdvice(receiver);
    };
    
//    public static IDataArtist buildIDataArtist() {
//        return new MockDataArtist();
//    }
//
//    public static IDataAlbum buildIDataAlbum() {
//        return new MockDataAlbum();
//    }
//
//    public static Artist buildArtist(){
//        return new ArtistBean();
//    }
//
//    public static Album buildAlbum(){
//        return new AlbumBean();
//    }

}
